package com.inspector.app.ui.widget


import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ClickUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.example.commen.resource.CommentContect


/**
 * 布局属性注解
 */
object BindingAdapter {

    @BindingAdapter("imageUrl")
    @JvmStatic
    fun loadImage(imageView: ImageView, url: Any?) {
        if (url == null) return
        Glide.with(imageView.context).load(url).into(imageView)
    }

    @BindingAdapter("checkText")
    @JvmStatic
    fun setCheckText(textView: TextView, content: String?) {
        if (content.isNullOrEmpty()) {
            textView.text = "--"
        } else textView.text = content
    }


    @BindingAdapter(value = ["imageUrl", "defaultImage"], requireAll = false)
    @JvmStatic
    fun loadImageWithDefault(imageView: ImageView, url: String?, defaultDrawRes: Int) {
        Glide.with(imageView.context)
            .setDefaultRequestOptions(RequestOptions().placeholder(defaultDrawRes).error(defaultDrawRes))
            .load(url)
            .into(imageView)
    }

    @JvmStatic
    fun loadImage(imageView: ImageView, url: Any?, defaultDrawRes: Int) {
        Glide.with(imageView.context)
            .setDefaultRequestOptions(RequestOptions().placeholder(defaultDrawRes).error(defaultDrawRes))
            .load(url)
            .into(imageView)
    }

    @BindingAdapter("loadSrc")
    @JvmStatic
    fun bindImageViewResource(imageView: ImageView, resource: Int?) {
        if (resource == null) return
        imageView.setImageResource(resource)
    }

    @BindingAdapter("visibleGone")
    @JvmStatic
    fun showHide(view: View, show: Boolean) {
        view.visibility = if (show) View.VISIBLE else View.GONE
    }

    @BindingAdapter("matchConstraintPercentWidth")
    @JvmStatic
    fun setMatchConstraintPercentWidth(view: View, percent: Float) {
        val params = view.layoutParams as ConstraintLayout.LayoutParams
        params.matchConstraintPercentWidth = percent
        view.layoutParams = params
    }

    @BindingAdapter("layoutWidth")
    @JvmStatic
    fun ViewGroup.setLayoutWidth(dimen: Float) {
        layoutParams.width = dimen.toInt()
    }

    @BindingAdapter("layoutHeight")
    @JvmStatic
    fun ViewGroup.setLayoutHeight(dimen: Float) {
        layoutParams.height = dimen.toInt()
    }


    @BindingAdapter("afterTextChanged")
    @JvmStatic
    fun setAfterTextChangedListener(editText: EditText, listener: AfterTextChangedListener?) {
        editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                listener?.afterTextChanged(editText)
            }
        })
    }

    interface AfterTextChangedListener {
        fun afterTextChanged(editText: EditText)
    }

    @BindingAdapter("adapter")
    @JvmStatic
    fun setRecyclerViewAdapter(recyclerView: RecyclerView, adapter: RecyclerView.Adapter<*>) {
        recyclerView.adapter = adapter
    }


    @BindingAdapter("onSafeClick")
    @JvmStatic
    fun onSafeClick(view: View, listener: View.OnClickListener) {
        ClickUtils.applyGlobalDebouncing(view, CommentContect.config.clickDelayTime, listener)
    }

    fun View.safeClick(listener: View.OnClickListener) {
        ClickUtils.applyGlobalDebouncing(this, CommentContect.config.clickDelayTime, listener)
    }


    fun loadImageWithListener(imageView: ImageView, url: Any?) {
        Glide.with(imageView.context)
            .setDefaultRequestOptions(RequestOptions().placeholder(imageView.drawable).error(imageView.drawable))
            .load(url)
            .into(object: SimpleTarget<Drawable>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {
                    imageView.setImageDrawable(resource)
                }
            })
    }

}
